<template>
  <div class="oe-info-top">
    <div class="oe-infotop-left" @click="toggleCollapsed" :style="{ textAlign: showExpend ? '' : 'center' }">
      <div class="oe-info-logoUrl" v-if="logoUrl">
        <img :src="logoUrl" :title="schoolName" />
      </div>
      <div class="oe-info-schoolName" v-else>
        <span>{{ schoolName }}</span>
      </div>
      <!-- <span class="oe-left-logo"></span> -->
      <!-- <span class="iconfont icon-zhankai1" :class="{ close: collapsed }" v-if="showExpend"></span> -->
    </div>
    <div class="text"><span>智慧教育考试平台</span></div>
    <!-- <div class="oe-info-logoUrl" v-if="logoUrl">
      <img :src="logoUrl" :title="schoolName" />
    </div>
    <div class="oe-info-schoolName" v-else>
      <span>{{ schoolName }}</span>
    </div> -->
    <div class="oe-infotop-right">
      <a class="oe-top-help" href="https://help.icve.com.cn/exam/" target="_blank">帮助中心</a>
      <!-- <span
        :class="['oe-right-level', level === 1 ? 'color-experience' : level === 2 ? 'color-free' : 'color-normal']"
      >{{ level == 1 ? `高配版(${duringTime})` : `低配版(${duringTime})` }}</span> -->
      <div class="fl" style="margin-right: 50px" v-if="majorBatch">
        <a-button type="primary" @click="getTicket" v-if="IsShowTicket" style="margin-right: 20px">打印准考证</a-button>
        <div class="select-box" v-if="isHavaMajor">
          考试分类：
          <a-select
            placeholder="请选择分类"
            v-model="sortId"
            optionFilterProp="children"
            style="width: 200px; margin-bottom: 20px"
            @change="handleChange"
          >
            <a-select-option v-for="(item, index) in sortList" :index="index" :key="item.Id">{{
              item.Title
            }}</a-select-option>
          </a-select>
        </div>
        <!-- <a-button type="primary" v-if="isHavaMajor" @click="getMajorBatch(0)">切换分类</a-button> -->
        <div class="select-box">
          考试批次：
          <a-select
            placeholder="请选择批次"
            v-model="bacthId"
            optionFilterProp="children"
            style="width: 200px; margin-bottom: 20px"
            @change="handleChangeBacth"
          >
            <a-select-option v-for="(item, index) in bacthList" :index="index" :key="item.Id">{{
              item.Title
            }}</a-select-option>
          </a-select>
        </div>
        <!-- <a-button type="primary" style="margin-left:15px;" @click="getMajorBatch(1)">切换批次</a-button> -->
      </div>
      <span class="oe-right-welcome">欢迎您！{{ displayName }}</span>
      <a-dropdown v-if="examShow">
        <a class="ant-dropdown-link" style="height: 40px">
          <span class="oe-right-avatar" :style="{ background: 'url(' + avatarUrl || headImg + ');' }"></span>
          <a-icon type="down" />
        </a>
        <a-menu slot="overlay">
          <a-menu-item v-if="isShowManager">
            <a href="javascript:;" @click="getTea">管理空间</a>
          </a-menu-item>
          <a-menu-item v-if="isShowStudent">
            <a href="javascript:;" @click="getStu">学生空间</a>
          </a-menu-item>
          <a-menu-item>
            <a href="javascript:;" style="color: #f00" @click="loginOut">退出登录</a>
          </a-menu-item>
        </a-menu>
      </a-dropdown>
      <!-- <a class="ant-dropdown-link" style="height: 40px" v-else>
        <span class="oe-right-avatar" :style="{ background: 'url(' + avatarUrl || headImg + ');' }"></span>
      </a> -->
    </div>
  </div>
</template>

<script>
export default {
  name: 'infoTop',
  props: {
    showExpend: {
      type: Boolean,
      default: true,
    },
    examShow: {
      type: Boolean,
      default: true,
    },
    majorBatch: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      collapsed: false,
      headImg: 'https://iconfont.alicdn.com/t/1566197395754.jpg@100h_100w.jpg',
      avatarUrl: '',
      displayName: '',
      logoUrl: localStorage.getItem('logoUrl'),
      schoolName: localStorage.getItem('schoolName'),
      isShowManager: false,
      isShowStudent: false,
      // level: 1,
      // duringTime: '--',
      sortId: localStorage.getItem('sortId') || '',
      sortList: [],
      bacthId: localStorage.getItem('bacthId') || '',
      bacthList: [],
      isHavaMajor: false,
      IsShowTicket: false,
    };
  },
  created() {
    this.getAvatorInfo()
    this.$bus.$off('getSchoolLogo').$on('getSchoolLogo', (val) => {
      console.log(val);
      this.logoUrl = val;
    });
  },
  mounted() {
    this.getManagerInfo();
    if (this.examShow && this.majorBatch) {
      this.getList();
      this.getBacthList();
    }
  },
  methods: {
    toggleCollapsed() {
      this.collapsed = !this.collapsed;
      this.$emit('toggleCollapsed', this.collapsed);
    },
    getManagerInfo() {
      this.$http.post('/api/common/LocalStorage/getHeadInfo').then((res) => {
        if (res.code < 0) {
          this.$message.warning(res.msg);
          this.$router.push({
            name: 'login',
            params: {},
          });
        } else {
          // this.level = res.level;
          // this.duringTime = res.endTime;
          // console.log('xuxuxu',res);
          // var rx = /^https?:\/\//i;
          // if (rx.test(res.avatarUrl)) {

          //   this.avatarUrl = res.avatarUrl;
          // } else {
          //   this.avatarUrl = 'https://cdn-zjy.icve.com.cn' + res.avatarUrl;
          // }
          this.displayName = res.displayName;
          localStorage.setItem('file_system_url', res.file_system_url);
          this.isShowManager = res.isShowManager;
          this.isShowStudent = res.isShowStudent;
          this.isHavaMajor = res.isHavaMajor;
        }
      });
    },
    getAvatorInfo() {
      this.$http.post('/api/common/accounts/getManagerInfo').then((res) => {
        if (res.code > 0) {
          var rx = /^https?:\/\//i;
          if (rx.test(res.avatarUrl)) {
            this.avatarUrl = res.avatarUrl;
          } else {
            this.avatarUrl = 'https://cdn-zjy.icve.com.cn' + res.avatarUrl;
          }
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    loginOut() {
      this.$http.post('/api/portal/loginforour/loginOut').then((res) => {
        if (res.code > 0) {
          this.$message.success(res.msg);
          this.$router.push({
            name: 'login',
            params: {},
          });
        }
      });
    },
    getMajorBatch(type) {
      let name = '';
      if (type == 0) {
        name = 'majorClass';
      } else if (type == 1) {
        name = 'batchList';
      }
      this.$router.push({
        name: name,
      });
    },
    getTea() {
      this.$router.push({ name: 'baseInfo' });
    },
    getStu() {
      this.$router.push({ name: 'majorClass' });
    },
    // 分类列表
    getList() {
      this.$http.post('/api/study/sort/getList').then((res) => {
        if (res.list.length > 0) {
          this.sortList = res.list;
          if (!this.sortId) {
            this.sortId = this.sortList[0].Id;
            localStorage.setItem('sortId', this.sortId);
          }
        } else {
          localStorage.setItem('sortId', '');
        }
      });
    },
    handleChange(value) {
      this.sortId = value;
      localStorage.setItem('sortId', value);
      this.changeChild();
      console.log(this.sortList);
      this.updateTitle(value);
    },
    updateTitle(value) {
      let filter = this.sortList.filter((item) => {
        return item.Id == value;
      });
      console.log(filter[0]);
      localStorage.setItem('sortItem', JSON.stringify(filter[0]));
    },
    // 批次列表
    getBacthList() {
      this.$http
        .post('/api/study/batch/getList', {
          title: '',
          type: 1,
        })
        .then((rst) => {
          if (rst.code > 0) {
            this.bacthList = rst.list;
            if (!this.bacthId) {
              this.bacthId = this.bacthList[0].Id;
              localStorage.setItem('bacthId', this.bacthId);
            }
            this.getTicketStatus();
          } else {
            this.$message.error(rst.msg);
          }
        });
    },
    handleChangeBacth(value) {
      this.bacthId = value;
      localStorage.setItem('bacthId', value);
      this.changeChild();
      this.getTicketStatus();
    },
    getTicketStatus() {
      this.$http
        .post('/api/study/batch/getTicket', {
          batchId: this.bacthId,
        })
        .then((rst) => {
          this.IsShowTicket = rst.isShowTicket;
        });
    },
    changeChild() {
      this.$emit('changeChild', false);
      setTimeout(() => {
        this.$emit('changeChild', true);
      }, 500);
    },
    getTicket() {
      this.$router.push({
        name: 'stuTicket',
      });
    },
  },
};
</script>
